#include "menu.h"
#include "ui_menu.h"
#include"addmenu.h"
#include"getinftonet.h"
menu::menu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::menu)
{
    ui->setupUi(this);
    network_shopinfimg = new QNetworkAccessManager();
    network_shopinfimgreq = new QNetworkRequest();
    connect(network_shopinfimg, &QNetworkAccessManager::finished, this, &menu::getshopinftoimg);

        //setStyleSheet("background-color:orange;border-radius 15px");
    ui->type->hide();
    ui->minet->hide();
    ui->minetl->hide();
    ui->typel->hide();
}

menu::~menu()
{
    delete ui;
}


void menu::init(mymenu &menu){
    menus = menu;

    ui->name->setText(menus.name);
    ui->hot->setText(menus.hot);
    //ui->minet->setText(menus.minet);
    ui->money->setText(menus.money);
    //ui->eattype->setText(menus.eattype);
    if(menus.eattype[0]=="1") ui->checkBoxh->setChecked(true);
    else ui->checkBoxh->setChecked(false);
    if(menus.eattype[1]=="1") ui->checkBoxl->setChecked(true);
    else ui->checkBoxl->setChecked(false);
    if(menus.eattype[2]=="1") ui->checkBoxe->setChecked(true);
    else ui->checkBoxe->setChecked(false);
    network_shopinfimgreq->setUrl(QUrl(menus.photo.data()));
    network_shopinfimg->get(*network_shopinfimgreq);
//ui->img->setStyleSheet(QString("border-image:url(%1)").arg(menu.photo.data()));

}

void menu::getshopinftoimg(QNetworkReply *reply){
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QPixmap * P = new QPixmap;
        P->loadFromData(bytes);
        //ui->img->setPixmap(*P);
        ui->img->setPixmap(*P);
        ui->img->setScaledContents(true);
        ui->img->show();
        delete P;
    }
}

void menu::on_xiugai_clicked()
{
    mymenu menu;



    menu.hot = "0";
    menu.sid = shopunit;
    menu.name = ui->name->text();
    menu.money =ui->money->text();
    menu.type = ui->type->text();
    menu.minet = ui->minet->text();

if(ui->checkBoxh->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");
if(ui->checkBoxl->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");
if(ui->checkBoxe->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");

QByteArray buf;
buf.append(QString("{"
           "\"id\":\"%1\","
           "\"name\":\"%2\","
           "\"hot\":\"%3\","
           "\"minet\":\"%4\","
           "\"eattype\":\"%5\","
            "\"money\":\"%6\","
            "\"type\":\"%7\","
            "\"imgtype\":\"jpg\","
           "\"length\":\"%8\"")
           .arg(menus.id).arg(menu.name).arg(menu.hot)
           .arg(menu.minet).arg(menu.eattype).arg(menu.money).arg(menu.type)
           .arg(QString::number(img.length())).toUtf8());
if(img.length()>100){
    buf.append(",\"photo\":\"");
    buf+=img;
}
buf.append("\"}");
Getinftonet reqpost;
reqpost.topost("http://hunya.fun:9000/cliupmenu",buf);
img.clear();
}
